package com.hqbzl.chat.handler;

import com.hqbzl.chat.message.PingMessage;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class HeartBeatHandler extends ChannelDuplexHandler {
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        IdleStateEvent event = (IdleStateEvent) evt;

        if (event.state() == IdleState.WRITER_IDLE) {
            log.debug("已经3秒没写到数据了， 发心跳包");
            ctx.writeAndFlush(new PingMessage("ping"));
        }

        super.userEventTriggered(ctx, evt);
    }
}
